package com.ruoyi.contact.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.contact.domain.ContactLevel;
import com.ruoyi.contact.service.IContactLevelService;
import com.ruoyi.contact.service.IContactReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class ContactReportController
{
    private String prefix = "contact/report";

    @Autowired
    private IContactReportService contactReportService;

    @RequiresPermissions("contact:report:view")
    @GetMapping("/contact/toReport")
    public String toReport(ModelMap modelMap)
    {
        List<Map<String, Integer>> countByCategory = contactReportService.selectContactCountByCategory();
        modelMap.put("dataCountByCategory", countByCategory);
        return "contact/report/report";
    }

    /**
     * 查询联系人等级列表
     */
    @RequiresPermissions("contact:report:view")
    @GetMapping("/contact/drawPie")
    public String drawPie(ModelMap modelMap)
    {
        List<Map<String, Integer>> countByCategory = contactReportService.selectContactCountByCategory();
        modelMap.put("dataCountByCategory", countByCategory);
        return "contact/report/pie";
    }

    @RequiresPermissions("contact:report:view")
    @GetMapping("/contact/drawBar")
    public String toReport1(ModelMap modelMap)
    {
        List<Map<String, Integer>> countByCategory = contactReportService.selectContactCountByCategory();
        modelMap.put("dataCountByCategory", countByCategory);
        return "contact/report/bar";
    }

    @RequiresPermissions("contact:report:view")
    @GetMapping("/contact/monthly")
    public String monthlyReport(ModelMap mmap){
        System.err.println("monthlyReport...");
        List<Map<String, Object>> data = contactReportService.selectContactCountByMonth();

        List<String> months = new ArrayList<>();
        List<Long> counts = new ArrayList<>();
        for (Map<String, Object> row : data){
            months.add((String) row.get("month"));
            counts.add(((Number) row.get("count")).longValue());
        }

        mmap.put("months", months);
        mmap.put("counts", counts);
        return "contact/report/monthly";
    }

}
